<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" v-show="showSearch" :inline="true" label-width="100px">
      <el-form-item label="店铺名称" prop="shopsName">
        <el-input v-model="queryParams.shopsName" placeholder="请输入店铺名称" clearable style="width: 240px" @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="Plus" @click="handleAdd">新增</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>
    <!-- 表格数据 -->
    <el-table v-loading="loading" :scrollbar-always-on="true" :data="groupList">
      <el-table-column label="店铺编码" prop="shopsNo" width="300" />
      <el-table-column label="店铺名称" prop="shopsName" width="300" />
      <el-table-column label="店铺站点" prop="shopsSite" :show-overflow-tooltip="true"></el-table-column>
      <el-table-column label="客服人员" prop="shopsServiceName" :show-overflow-tooltip="true" width="200" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" fixed="right" width="200">
        <template #default="scope">
          <el-button link type="primary" @click="handleUpdate(scope.row)">修改</el-button>
          <el-button link type="primary" @click="handleDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum" v-model:limit="queryParams.pageSize" @pagination="getList" />

    <!-- 添加或修改店铺配置对话框 -->
    <el-dialog :title="title" v-model="open" width="700px" append-to-body>
      <el-form ref="roleRef" :model="form" :rules="rules" label-width="130px">
        <el-form-item label="店铺编码" prop="shopsNo">
          <el-input v-model="form.shopsNo" placeholder="请输入店铺编码" />
        </el-form-item>
        <el-form-item label="店铺名称" prop="shopsName">
          <el-input v-model="form.shopsName" placeholder="请输入店铺名称" />
        </el-form-item>
        <el-form-item label="店铺站点" prop="shopsSite">
          <el-input v-model="form.shopsSite" placeholder="请输入店铺站点" />
        </el-form-item>
        <el-form-item label="客服人员" prop="shopsServiceId">
          <span @click="selectKf">{{form.shopsServiceId?form.shopsServiceName:'请选择客服人员'}}</span>
        </el-form-item>
        <el-form-item label="打印模板" prop="shopsPrintTemplate">
          <el-select v-model="form.shopsPrintTemplate" placeholder="请选择打印模板" clearable style="width: 200px">
            <el-option v-for="dict in print_template" :key="dict.value" :label="dict.label" :value="dict.value" />
          </el-select>
        </el-form-item>
        <el-form-item label="APP_Key" prop="shopsAppKey">
          <el-input v-model="form.shopsAppKey" placeholder="请输入APP_Key" />
        </el-form-item>
        <el-form-item label="APP_SECRET" prop="appSecret">
          <el-input v-model="form.shopsAppSecret" placeholder="请输入APP_SECRET" />
        </el-form-item>
        <el-form-item label="ACCESS_TOKEN" prop="shopsAccessToken">
          <el-input v-model="form.shopsAccessToken" placeholder="请输入ACCESS_TOKEN" />
        </el-form-item>
        <el-form-item label="备注说明" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注说明" type="textarea" />
        </el-form-item>
      </el-form>
      <el-dialog title="选择用户" v-model="visible" width="900px" top="7vh">
        <el-form :model="UserqueryParams" ref="queryRef" :inline="true">
          <el-form-item label="用户名称" prop="userName">
            <el-input v-model="UserqueryParams.userName" placeholder="请输入用户名称" clearable style="width: 200px" @keyup.enter="userhandleQuery" />
          </el-form-item>
          <el-form-item label="手机号码" prop="phonenumber">
            <el-input v-model="UserqueryParams.phonenumber" placeholder="请输入手机号码" clearable style="width: 200px" @keyup.enter="userhandleQuery" />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="Search" @click="userhandleQuery">搜索</el-button>
            <el-button icon="Refresh" @click="userresetQuery">重置</el-button>
          </el-form-item>
        </el-form>
        <el-row>
          <el-table ref="refTable" :data="userList" @row-click="userhandleSelectionChange" height="260px">
            <el-table-column label="用户名称" prop="userName" :show-overflow-tooltip="true" />
            <el-table-column label="用户昵称" prop="nickName" :show-overflow-tooltip="true" />
            <el-table-column label="手机" prop="phonenumber" :show-overflow-tooltip="true" />
            <el-table-column label="状态" align="center" prop="status">
              <template #default="scope">
                <dict-tag :options="sys_normal_disable" :value="scope.row.status" />
              </template>
            </el-table-column>
          </el-table>
          <pagination v-show="usertotal > 0" :total="usertotal" v-model:page="UserqueryParams.pageNum" v-model:limit="UserqueryParams.pageSize"
            @pagination="usergetList" />
        </el-row>
        <template #footer>
          <div class="dialog-footer">
            <el-button type="primary" @click="handleSelectUser">确 定</el-button>
            <el-button @click="visible = false">取 消</el-button>
          </div>
        </template>
      </el-dialog>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
    <!-- 选择用户 -->

  </div>
</template>

<script setup name="store">
  import { listType } from "@/api/order/type";
  import {
    list,
    add,
    edit,
    remove,
    info
  } from "@/api/order/store"
  import { listUser } from '@/api/system/user'
  import {
    listapplicant,
  } from "@/api/system/applicant";
  import baseForm from "../hooks/useOrderBase";
  const { headers, actionUrl, fileList, fileList2, fileList3 } = baseForm();

  const router = useRouter();
  const route = useRoute()
  const { proxy } = getCurrentInstance();
  const { print_template } = proxy.useDict("print_template");
  const lqValue = ref([{
    value: 'Y',
    label: '是'
  }, {
    value: 'N',
    label: '否'
  }])
  const visible = ref(false)
  const TypeList = ref([])
  const groupList = ref([]);
  const open = ref(false);
  const appLicationList = ref([])
  const loading = ref(true);
  const showSearch = ref(true);
  const ids = ref([]);
  const single = ref(true);
  const multiple = ref(true);
  const total = ref(0);
  const title = ref("");
  const dateRange = ref([]);
  const menuOptions = ref([]);
  const menuExpand = ref(false);
  const menuNodeAll = ref(false);
  const deptExpand = ref(true);
  const deptNodeAll = ref(false);
  const deptOptions = ref([]);
  const openDataScope = ref(false);
  const menuRef = ref(null);
  const deptRef = ref(null);
  const userList = ref([])
  const usertotal = ref(null)

  function selectKf() {
    visible.value = true;
    usergetList()
  }
  /** 查询分类列表 */
  function getTypeList() {
    loading.value = true;
    listType(queryParams.value).then(response => {
      TypeList.value = proxy.handleTree(response.data, "id", "categoryParentId");
    });
  }
  // 多选框选中数据
  function userhandleSelectionChange(row) {
    form.value.shopsServiceName = row.userName
    form.value.shopsServiceId = row.userId
    visible.value = false
  }
  /** 搜索按钮操作 */
  function userhandleQuery() {
    UserqueryParams.pageNum = 1
    usergetList()
  };
  // 查询表数据
  function usergetList() {
    listUser({ ...UserqueryParams.value }).then(res => {
      userList.value = res.rows
      usertotal.value = res.total
    })
  };
  /** 重置按钮操作 */
  function userresetQuery() {
    proxy.resetForm('queryRef')
    userhandleQuery()
  }
  const data = reactive({
    form: {},
    UserqueryParams: {
      pageNum: 1,
      pageSize: 10,
    },
    queryParams: {
      pageNum: 1,
      pageSize: 10,
      shopsName: null
    },
    rules: {
      shopsNo: [{ required: true, message: "店铺编码不能为空", trigger: "blur" }],
      shopsName: [{ required: true, message: "店铺名称不能为空", trigger: "blur" }],
      shopsSite: [{ required: true, message: "店铺站点不能为空", trigger: "blur" }],
      shopsPrintTemplate: [{ required: true, message: "打印模板不能为空", trigger: "blur" }],
    },
  });
  const { queryParams, form, rules, UserqueryParams } = toRefs(data);

  /** 查询店铺列表 */
  function getList() {
    loading.value = true;
    let formQueryParam = { ...queryParams.value };
    formQueryParam.expireTemp = formQueryParam.expireTemp == 0 ? '' : formQueryParam.expireTemp
    list(proxy.addDateRange(formQueryParam, dateRange.value)).then(response => {
      groupList.value = response.rows
      total.value = response.total;
      loading.value = false;
    });
  }

  /** 搜索按钮操作 */
  function handleQuery() {
    queryParams.value.pageNum = 1;
    getList();
  }
  /** 重置按钮操作 */
  function resetQuery() {
    dateRange.value = [];
    queryParams.value = {
      pageNum: 1,
      pageSize: 10,
      expireTemp: '',
      styleName: undefined,
      styleLinkName: undefined,
      styleLinkMobile: '',
      styleExpireStatus: '',
      styleAvl: ''
    }
    handleQuery();
  }

  /** 删除按钮操作 */
  function handleDelete(row) {
    const ids = row.id || ids.value;
    proxy.$modal.confirm('是否确认删除店铺编号为"' + row.shopsNo + '"的数据项?').then(function() {
      return remove(ids);
    }).then(() => {
      getList();
      proxy.$modal.msgSuccess("删除成功");
    }).catch(() => {});
  }


  /** 重置新增的表单以及其他数据  */
  function reset() {
    if (menuRef.value != undefined) {
      menuRef.value.setCheckedKeys([]);
    }
    menuExpand.value = false;
    menuNodeAll.value = false;
    deptExpand.value = true;
    deptNodeAll.value = false;
    form.value = {
      styleName: undefined,
      styleLinkName: undefined,
      styleLinkMobile: '',
      styleExpireStatus: 'F',
      styleAvl: 'Y',
      tenantCreateParams: []
    };
    proxy.resetForm("roleRef");
  }

  /** 添加店铺 */
  function handleAdd() {
    reset();
    open.value = true;
    title.value = "添加店铺";
  }

  /** 修改店铺 */
  function handleUpdate(row) {
    reset();
    info({
      id: row.id
    }).then((res) => {
      form.value = { ...res.data };
      open.value = true;
      title.value = "修改店铺";
    })
  }

  /** 提交按钮 */
  function submitForm() {
    let pass = true
    if (!form.value.id && form.value.tenantCreateParams && form.value.tenantCreateParams.length) {
      form.value.tenantCreateParams.forEach((a, index) => {
        if (!a.applicantId || !a.tenantCode || !a.tenantName) {
          pass = false
        }
      })
    }
    if (!pass) {
      proxy.$modal.msgWarning("物料列表下内容不能有空~");
      return
    }
    // if (form.value.styleExpireStatus == 'T' && !form.value.styleExpireDate) {
    //   proxy.$modal.msgWarning("请选择截止日期~");
    //   return
    // }
    proxy.$refs["roleRef"].validate(valid => {
      if (valid) {
        if (form.value.id != undefined) {
          edit(form.value).then(response => {
            proxy.$modal.msgSuccess("修改成功");
            open.value = false;
            getList();
          });
        } else {
          add(form.value).then(response => {
            proxy.$modal.msgSuccess("新增成功");
            open.value = false;
            getList();
          });
        }
      }
    });
  }

  /** 取消按钮 */
  function cancel() {
    open.value = false;
    reset();
  }
  getList();
</script>